package com.caffedev.ottollotto;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class OttollottoStatsView extends ListActivity {

	public static final int ID=0;
	public static final int INDEX_GRWNO=1;
	public static final int INDEX_GRWDATE=2;
	public static final int INDEX_FIRSTBALL=3;
	public static final int INDEX_SECONDBALL=4;
	public static final int INDEX_THIRDBALL=5;
	public static final int INDEX_FOURTHBALL=6;
	public static final int INDEX_FIFTHBALL=7;
	public static final int INDEX_SIXTHBALL=8;
	public static final int INDEX_BONUSBALL=9;
	
	private static final String TAG = "OttollottoStatsView";
	private TextView title;
	private ImageView img;
	
	
	//private ImageAdapter wAdapter;
	private ListAdapter vAdapter;
	private ArrayList<Integer> lines;
	private ArrayList<String> oddLines;
	private Cursor vCursor;
	
	
	private int max_count=0;
	
	private static final int COUNT_BY_NUM = 45; 
	private static final int COUNT_BY_COLOR = 5;
	private static final int COUNT_BY_SEC5 = 9;
	private static final int COUNT_BY_SEC10 = 5;
	private static final int COUNT_BY_ODD = 45;
	private int COUNT_BY_RECENT = 0;
	
	private String mode;
	private String from_str;
	private String to_str;
	private int from_int;
	private int to_int;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		setContentView(R.layout.statsview);
		
		title = (TextView)findViewById(R.id.textView1);

		img = (ImageView)findViewById(R.id.imageView1);
		
		Intent intent = getIntent();
		mode = intent.getStringExtra("mode");
		from_str = intent.getStringExtra("from");
		to_str = intent.getStringExtra("to");
		
		from_int = Integer.valueOf(from_str.substring(0, from_str.indexOf(getString(R.string.round))));
		to_int = Integer.valueOf(to_str.substring(0, to_str.indexOf(getString(R.string.round))));
		
		
		lines = new ArrayList<Integer>();
		oddLines = new ArrayList<String>();
		
		title.setText(from_str + getString(R.string.from)+" "+to_str + getString(R.string.to));
		
		
		
		
		vCursor = OttollottoMain.mCursor;
		vCursor.moveToFirst();
		
		if(mode.equalsIgnoreCase(getString(R.string.bynumber))){
			img.setBackgroundResource(R.drawable.bynum);
			byNumber();
		}
		else if(mode.equalsIgnoreCase(getString(R.string.bysection10))){
			img.setBackgroundResource(R.drawable.bysec);
			bySec10();
		}
		else if(mode.equalsIgnoreCase(getString(R.string.bysection5))){
			img.setBackgroundResource(R.drawable.bysec);
			bySec5();
		}
		else if(mode.equalsIgnoreCase(getString(R.string.bycolor))){
			img.setBackgroundResource(R.drawable.bycolor);
			bySec10();
		}
		else if(mode.equalsIgnoreCase(getString(R.string.byodd))){
			img.setBackgroundResource(R.drawable.byodd);
			byOdd();
		}
		else if(mode.equalsIgnoreCase(getString(R.string.byrecent))){
			img.setBackgroundResource(R.drawable.byrecent);
			byRecent();
		}
		else
			Log.e(TAG,"error");
		
		
	}
	private void byNumber(){
		
		refreshNumbers(COUNT_BY_NUM);//set numbers[] to 0
	
		while(!vCursor.isAfterLast() ){
			int no = Integer.valueOf(vCursor.getString(INDEX_GRWNO));
			if( from_int <= no && no <= to_int ){
				for(int i = INDEX_FIRSTBALL ; i<= INDEX_BONUSBALL; i++){
					int o = Integer.valueOf(vCursor.getString(i))-1;
					lines.set(o, lines.get(o)+1);
				}
			}
			vCursor.moveToNext();
		}
		findMax();//set the max_count
		

		vAdapter = new ListAdapter(this);
		setListAdapter(vAdapter);
		
		
	}
	private void bySec10(){
		refreshNumbers(COUNT_BY_SEC10);//set numbers[] to 0
		
		while(!vCursor.isAfterLast() ){
			int no = Integer.valueOf(vCursor.getString(INDEX_GRWNO));
			if( from_int <= no && no <= to_int ){
				for(int i = INDEX_FIRSTBALL ; i<= INDEX_BONUSBALL; i++){
					
					int o = Integer.valueOf(vCursor.getString(i));
					if(1<= o && o<= 10)
						lines.set(0, lines.get(0)+1);
					else if(11<=o && o<=20)
						lines.set(1, lines.get(1)+1);
					else if(21<= o && o<=30)
						lines.set(2, lines.get(2)+1);
					else if(31<= o && o<=40)
						lines.set(3, lines.get(3)+1);
					else 
						lines.set(4, lines.get(4)+1);
					
				}
			}
			vCursor.moveToNext();
		}

		findMax();//set the max_count
		
		vAdapter = new ListAdapter(this);
		setListAdapter(vAdapter);
	}
	private void bySec5(){

		refreshNumbers(COUNT_BY_SEC5);//set numbers[] to 0
		
		while(!vCursor.isAfterLast() ){
			int no = Integer.valueOf(vCursor.getString(INDEX_GRWNO));
			if( from_int <= no && no <= to_int ){
				for(int i = INDEX_FIRSTBALL ; i<= INDEX_BONUSBALL; i++){
					
					int o = Integer.valueOf(vCursor.getString(i));
					if(1<= o && o<= 5)
						lines.set(0, lines.get(0)+1);
					else if(6<=o && o<=10)
						lines.set(1, lines.get(1)+1);
					else if(11<= o && o<=15)
						lines.set(2, lines.get(2)+1);
					else if(16<= o && o<=20)
						lines.set(3, lines.get(3)+1);
					else if(21<= o && o<=25)
						lines.set(4, lines.get(4)+1);
					else if(26<= o && o<=30)
						lines.set(5, lines.get(5)+1);
					else if(31<= o && o<=35)
						lines.set(6, lines.get(6)+1);
					else if(36<= o && o<=40)
						lines.set(7, lines.get(7)+1);
					else 
						lines.set(8, lines.get(8)+1);
					
				}
			}
			vCursor.moveToNext();
		}

		findMax();//set the max_count
		
		vAdapter = new ListAdapter(this);
		setListAdapter(vAdapter);
		
	}
/*	private void byColor(){
		
	}*/
	private void byOdd(){

		//Log.e(TAG,"byOdd");
		if(!oddLines.isEmpty())
			oddLines.clear();
	
		int odd_count=0;
		int even_count = 0;
		int sum = 0;
		
		while(!vCursor.isAfterLast() ){
			odd_count=even_count=sum=0;
			int no = Integer.valueOf(vCursor.getString(INDEX_GRWNO));	
			if( from_int <= no && no <= to_int ){
				for(int i = INDEX_FIRSTBALL ; i<= INDEX_BONUSBALL; i++){
					int o = Integer.valueOf(vCursor.getString(i));
					
					sum+=o;
					if( o%2 == 0 ){//even
						even_count++;
					}else
						odd_count++;
					//lines.set(o, lines.get(o)+1);
				}
				oddLines.add(no+" "+odd_count+" "+even_count+" "+sum );
			}
			vCursor.moveToNext();
		}
		
		//findMax();//set the max_count
		

		vAdapter = new ListAdapter(this);
		setListAdapter(vAdapter);
		
	}
	private void byRecent(){

		
		refreshNumbers(COUNT_BY_NUM);//set numbers[] to 0
	
		while(!vCursor.isAfterLast() ){
			int no = Integer.valueOf(vCursor.getString(INDEX_GRWNO));
			if( from_int <= no && no <= to_int ){
				for(int i = INDEX_FIRSTBALL ; i<= INDEX_BONUSBALL; i++){
					int o = Integer.valueOf(vCursor.getString(i))-1;
					lines.set(o, lines.get(o)+1);
				}
			}
			vCursor.moveToNext();
		}
		findMax();//set the max_count
		

		vAdapter = new ListAdapter(this);
		setListAdapter(vAdapter);
		
		
	}
	

	private void findMax(){
		max_count = 0;

		for(int i = 0 ; i<lines.size();i++){
			if(max_count < lines.get(i))
				max_count = lines.get(i);
		}
	}
	
	private void refreshNumbers(int limit){
		lines.clear();
		for(int i = 0; i<limit;i++)
			lines.add(0);
	}

	
	private class ListAdapter extends BaseAdapter{
		private Context mContext;
		public ListAdapter(Context c){
			mContext = c;
			}
		@Override
		public int getCount() {
			if(mode.equalsIgnoreCase(getString(R.string.byodd)))
				return oddLines.size();
			else
				return lines.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			if(mode.equalsIgnoreCase(getString(R.string.byodd))){
				//Log.e(TAG,"odd : "+ oddLines.size());
				
				if(convertView == null){
					LayoutInflater inflater = LayoutInflater.from(mContext);
					convertView = (View)inflater.inflate(R.layout.statsrow_byodd, null);
				}
				TextView r = (TextView)convertView.findViewById(R.id.textView1);
				TextView o = (TextView)convertView.findViewById(R.id.textView2);
				TextView e = (TextView)convertView.findViewById(R.id.textView3);
				TextView s = (TextView)convertView.findViewById(R.id.textView4);
				
				String[] temp = oddLines.get(position).split(" ");
				
				r.setText(temp[0].toString() + getString(R.string.round));
				o.setText(temp[1].toString());
				e.setText(temp[2].toString());
				s.setText(temp[3].toString());
				//stat.setText(oddLines.get(position));
				
				
			}else{

				if(convertView == null){
					LayoutInflater inflater = LayoutInflater.from(mContext);
					convertView = (View)inflater.inflate(R.layout.statsrow, null);
		
				}
				
				ImageView ball = (ImageView)convertView.findViewById(R.id.imageView1);
				ProgressBar bar = (ProgressBar)convertView.findViewById(R.id.progressBar1);
				TextView stat = (TextView)convertView.findViewById(R.id.textView1);
				
				if(mode.equalsIgnoreCase(getString(R.string.bynumber)))
					ball.setBackgroundResource(getResources().getIdentifier("b" +(position+1), "drawable", getPackageName()));
				else if(mode.equalsIgnoreCase(getString(R.string.bysection5)))
					ball.setBackgroundResource(getResources().getIdentifier("sec5" +(position+1), "drawable", getPackageName()));
				else if(mode.equalsIgnoreCase(getString(R.string.bysection10)))
					ball.setBackgroundResource(getResources().getIdentifier("sec10" +(position+1), "drawable", getPackageName()));
				else if(mode.equalsIgnoreCase(getString(R.string.bycolor)))
					ball.setBackgroundResource(getResources().getIdentifier("color" +(position+1), "drawable", getPackageName()));
				else
					ball.setBackgroundResource(getResources().getIdentifier("b" +(position+1), "drawable", getPackageName()));
				
				bar.setProgress(lines.get(position)*100/max_count);
				//bar.setProgress(60);
				
				stat.setText(lines.get(position)+ getString(R.string.round));
				
				
				
			}


			return convertView;
			
		}
		
	}
}
